//
//  MixedMarksView.m
//  StraightTrain
//
//  Created by mk on 16/1/11.
//  Copyright © 2016年 mk. All rights reserved.
//

#import "MixedMarksView.h"

@implementation MixedMarksView

-(id)init
{
    self = [super init];
    if (self)
    {
        self.fram_X = 0;
        self.fram_Y = 0;
        self.fram_W = appScreenWidth;
        self.btn_H = 0;
        self.btn_W = 0;
        self.arrMarks = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.fram_X = 0;
        self.fram_Y = 0;
        self.fram_W = appScreenWidth;
        self.btn_H = 0;
        self.btn_W = 0;
        self.arrMarks = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    self.fram_X = 0;
    self.fram_Y = 0;
    self.fram_W = appScreenWidth;
    self.btn_H = 0;
    self.btn_W = 0;
    self.arrMarks = [[NSMutableArray alloc]initWithCapacity:0];
}

-(int)loadViewGetHighe
{
    for (UIView * viewBtn in [self subviews])
    {
        [viewBtn removeFromSuperview];
    }
    
    NSInteger Y = self.fram_Y;
    NSInteger X = self.fram_X;
    NSInteger viewW = self.fram_W;
    int btnW = 0;
    int btnH = 0;
    
    for (int i = 0; i<self.arrMarks.count; i++)
    {
        NSString * strTitle = [self.arrMarks objectAtIndex:i];
        CGSize sizeMax = [strTitle boundingRectWithSize:CGSizeMake(viewW,1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
        
        if (self.btn_W == 0)//不固定宽度
           btnW = (sizeMax.width<20?20:(sizeMax.width+15));
        else //固定宽度(第二排要和第一排对其)
           btnW = self.btn_W;
        
        if(self.btn_H == 0)//不固定高度
           btnH = (sizeMax.height< 20?20:(sizeMax.height+15));
        else
           btnH = self.btn_H;
        
        //固定宽度的或者宽度没有超过一行的
        if ((X+btnW+10) > viewW)
        {
            X = self.fram_X;
            Y = (Y + btnH + 10);
        }
        
        
        UIButton * btnMrk = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnMrk setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [btnMrk setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
        [btnMrk setTitleEdgeInsets:UIEdgeInsetsMake(0,5,0, 0)];
        btnMrk.titleLabel.font = [UIFont systemFontOfSize:14];
        btnMrk.titleLabel.numberOfLines = 1;
      
        [btnMrk setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btnMrk.tag = i;
        [btnMrk addTarget:self action:@selector(btnMixedMrkClick:) forControlEvents:UIControlEventTouchUpInside];
       
        btnMrk.frame = CGRectMake(X, Y,btnW,btnH);
        [self addSubview:btnMrk];
        
        X = (X+btnW+10);
    }
    
    return (Y+btnH);
}




-(void)btnMixedMrkClick:(id)sender
{
    if ([self.delegateMixed respondsToSelector:@selector(selectMixedMrk:)])
    {
        [self.delegateMixed selectMixedMrk:sender];
    }

}


@end








